#include <stdint.h>
#include <stdio.h>
#include "os.h"
#include "os_msg_queue.h"
#include "os_semaphore.h"
#include "os_thread.h"
#include <pthread.h>




pthread_t g_tid;

// os_semaphore_st g_semaphore = OS_SEMAPHORE_INIT(0,1);
uint8_t      g_buffer[100] = {0};
os_msg_queue_st g_msg_queue = OS_MSG_QUEUE_INIT(g_buffer, 100);

int task1(void * const me)
{
    static int i = 0;
    OS_TASK_BEGAIN
    while (1)
    {
        os_sleep(50); // Sleep for 1000 milliseconds
        os_msg_queue_send(&g_msg_queue, "hello world\n",sizeof("hello world\n"));
        //printf("task1: Sent message: %s\n", "hello world\n");
        i++;
    }
    OS_TASK_END
}

int task2(void * const me)
{
    int ret = -1;
    uint8_t msg[20] = {0};
    OS_TASK_BEGAIN
    while (1)
    {
        os_queue_receive_msg(&g_msg_queue, msg,13, 1000, &ret);
        if(ret)
        {
            printf("task2: Failed to receive message, ret = %d\n", ret);
            continue; // If timeout, continue to next iteration
        }
        printf("task2: Received message: %s\n", msg);

    }
    OS_TASK_END
}

int main(int argc, char *argv[])
{

    pthread_create(&g_tid, NULL, (void *)get_systick_thread, NULL); // Create a thread to get system tick

    os_thread_create(task1,NULL, 0);
    os_thread_create(task2, NULL,1);
    while(1)
    {

        os_thread_schedule();

    }
    return 0;
}


